<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:complain:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>
      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-select v-model="dataForm.category_id" :placeholder="i18n('common.please_select') + i18n('complain.category.title')" clearable>
              <el-option :label="i18n('common.all')" value=""></el-option>
              <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" label="#" width="70px"> </el-table-column>

          <el-table-column prop="category_id" :label="i18n('complain.category.title')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.category">{{ scope.row.category.title }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="content" :label="i18n('complain.content')">
          </el-table-column>

          <el-table-column :label="i18n('complain.nickname')" width="100">
            <template v-slot="scope">
              <span v-if="scope.row.member">{{ scope.row.member.nickname }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('complain.mobile')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.member">{{ scope.row.member.mobile }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('complain.email')" width="180">
            <template v-slot="scope">
              <span v-if="scope.row.member">{{ scope.row.member.email }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('complain.read_status')" width="100">
            <template v-slot="scope">
              {{ scope.row.read_status.text }}
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('complain.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="240">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:complain:read') && scope.row.read_status.value == 0"
                type="success"
                icon="View"
                @click="readHandle(scope.row.id)">
                {{ i18n('complain.read') }}
              </el-button>

              <el-button
                v-if="isAuth('module:complain:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="complain-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const refresh = inject("refresh")

  const model = ref('complain')

  const categoryList = ref([])

  const dataForm = reactive([
    'category_id',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)


  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/complain/category/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  const readHandle = (id) => {
    let message = '您已经阅读当前投诉信息？'

    $messagebox.confirm(message, i18n('common.prompt'), {
      confirmButtonText: i18n('common.confirm'),
      cancelButtonText: i18n('common.cancel'),
      type: 'warning',
    }).then(() => {
      $http({
        url: $http.adornUrl('/complain/read'),
        method: 'post',
        data: { id: id },
      }).then(({ data }) => {
        if (data && data.status === 200) {
          $message({
            message: i18n('common.handle_success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              refresh()
            },
          })
        } else {
          $message.error(i18n(data.message))
        }
      })
    }).catch(() => {})
  }

  onMounted(() => {
    getDataList(model)
    loadCategoryList()
  })
</script>
